using System;
using System.Windows.Forms;

namespace PhongKham
{
    static class Program
    {
        private static Class.ClsBaoLoi ms = new Class.ClsBaoLoi();
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            Chuoi.EncryptDecrypt ed = new Chuoi.EncryptDecrypt();
            if (global::PhongKham.Properties.Settings.Default.m_server.Trim() == "")
            {

                global::PhongKham.Properties.Settings.Default.m_server = ed.EncryptText("S2Q-PC\\SQLEXPRESS");
                global::PhongKham.Properties.Settings.Default.m_database = ed.EncryptText("QLPhongkham");
                global::PhongKham.Properties.Settings.Default.m_username = ed.EncryptText("S2Q");
                global::PhongKham.Properties.Settings.Default.m_password = ed.EncryptText("");
                global::PhongKham.Properties.Settings.Default.Save();
            }

#if(THO)
            global::PhongKham.Properties.Settings.Default.m_server = ed.EncryptText("TUYETBINH-PC\\SQLEXPRESS");
            global::PhongKham.Properties.Settings.Default.m_database = ed.EncryptText("Phongkham");
            global::PhongKham.Properties.Settings.Default.m_username = ed.EncryptText("Admin");
            global::PhongKham.Properties.Settings.Default.m_password = ed.EncryptText("admin");
            global::PhongKham.Properties.Settings.Default.Save();
#elif(DONG)
            global::PhongKham.Properties.Settings.Default.m_server = ed.EncryptText("S2Q-PC\\SQLEXPRESS");
            global::PhongKham.Properties.Settings.Default.m_database = ed.EncryptText("QLPhongkham");
            global::PhongKham.Properties.Settings.Default.m_username = ed.EncryptText("Admin");
            global::PhongKham.Properties.Settings.Default.m_password = ed.EncryptText("admin");
            global::PhongKham.Properties.Settings.Default.Save();
#elif(RELEASE)
            global::PhongKham.Properties.Settings.Default.m_server = ed.EncryptText("tgphuoc-pc\\sql2k8");
            global::PhongKham.Properties.Settings.Default.m_database = ed.EncryptText("HIS_DB_GD2");
            global::PhongKham.Properties.Settings.Default.m_username = ed.EncryptText("sa");
            global::PhongKham.Properties.Settings.Default.m_password = ed.EncryptText("sa123");
            global::PhongKham.Properties.Settings.Default.Save();
#elif(DEBUG)
            global::PhongKham.Properties.Settings.Default.m_server = ed.EncryptText("172.16.160.129\\sql2k8");
            global::PhongKham.Properties.Settings.Default.m_database = ed.EncryptText("HIS_DB_GD2");
            global::PhongKham.Properties.Settings.Default.m_username = ed.EncryptText("kho");
            global::PhongKham.Properties.Settings.Default.m_password = ed.EncryptText("ntuan");
            global::PhongKham.Properties.Settings.Default.Save();
#endif

            PhongKham.Class.ClsConnection ketnoi = new PhongKham.Class.ClsConnection();
            //if (ketnoi.KiemTraKetNoi())
            //{
                Application.Run(new FrmMain());

            //}            
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            if (e.Exception.Source == ".Net SqlClient Data Provider")
            {
                ms.Loi("Lỗi kết nối đến máy chủ dữ liệu.\nĐề nghị kiểm tra lại!");
            }
            else
            {
                MessageBox.Show("Hệ thống phát sinh lỗi, xin kiểm tra lại!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                Profile.Profile p = new Profile.Xml("Loi.xml");
                p.SetValue("Exception", e.Exception.Message, e.Exception);
            }
        }
    }
}